package com.hop.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableTest {

	public static void main(String[] args) {
		
		Callable<Integer> myCallable = new MyCallable(); // 创建MyCallable对象
		
		//使用FutureTask来包装MyCallable对象
		FutureTask<Integer> myFutureTask = new FutureTask<Integer>(myCallable);
		
		for(int i = 0; i<100; i++){
			System.out.println(Thread.currentThread().getName()+" "+i);
			if(i == 30){
				Thread th = new Thread(myFutureTask);
				th.start();				//FutureTask对象作为Thread对象的target创建新的线程
			}
		}
		System.out.println("主线程for循环执行完毕！！");
		
		try {
			int sum = myFutureTask.get();
			System.out.println("sum="+sum);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
